﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using static ConsoleApp1.TestDll;

namespace ConsoleApp1
{
    class Program
    {
        static bool sdk_init_finished = false;
        static int call_id = 1;
        static void sdk_init_cb(int ret, string jsonConfig) {
            Console.Write("application sdk_init_cb\n");
            if (ret == 0) {
                sdk_init_finished = true;
            }
        }

        static void sdk_listener_cb(string method, int argc, string[] argv)
        {
            if (method.Length > 0 && argc > 0) {
                Console.Write("method:" + method + " argv[0] = " + argv[0] + "\n");
            }

        }

        static void sdk_api_cb(int call_id, string api_method, string response)
        {
            if (api_method.Length > 0 && response.Length > 0)
            {
                Console.Write("call_id:" + call_id + "  api_method:" + api_method + "  response = " + response + "\n");
            }
        }


        static void Main(string[] args)
        {
            Console.WriteLine("Hello echatim sdk !");
            StringBuilder configJson = new StringBuilder("{\"host\":\"api.echatim.cn\",\"httpPort\":58082,\"socketPort\":59092,\"key\":\"TSDKTEST00001\",\"secret\":\"\",\"apiTransport\":\"HTTP\",\"loginAuid\":\"admin\",\"loginToken\":\"admin\"}");
            TestDll.sdk_init(configJson, new pfunc_init_cb(sdk_init_cb), new pfunc_listener_cb(sdk_listener_cb));
            while (sdk_init_finished == false) {
                Thread.Sleep(1000);
            }
            TestDll.sdk_set_api_listener(new pfunc_api_cb(sdk_api_cb));
            while (true) {
                TestDll.sdk_invoke_api(call_id, "userGet", "{\"auid\":\"admin\"}");
                call_id++;
                Thread.Sleep(1000);
            }
        }
    }

    class TestDll {
        public delegate void pfunc_init_cb(int ret, string jsonConfig);
        public delegate void pfunc_listener_cb(string method, int argc, [In, MarshalAs(UnmanagedType.LPArray, SizeConst=1, ArraySubType = UnmanagedType.LPStr)] string[] argv);
        public delegate void pfunc_api_cb(int call_id, string api_method, [In, MarshalAs(UnmanagedType.LPUTF8Str)] string response);

        [DllImport("C:\\Users\\Administrator\\CLionProjects\\echatim-client-cpp-core\\cmake-build-debug\\libs\\echatimsdk.dll")]
        public static extern void sdk_init(StringBuilder jsonConfig, pfunc_init_cb init_cb, pfunc_listener_cb listener_cb);
        [DllImport("C:\\Users\\Administrator\\CLionProjects\\echatim-client-cpp-core\\cmake-build-debug\\libs\\echatimsdk.dll")]
        public static extern void sdk_set_api_listener(pfunc_api_cb apit_cb);
        [DllImport("C:\\Users\\Administrator\\CLionProjects\\echatim-client-cpp-core\\cmake-build-debug\\libs\\echatimsdk.dll")]
        public static extern void sdk_invoke_api(int call_id, string api_method, [In, MarshalAs(UnmanagedType.LPUTF8Str)] string json_params);
    }

}
